using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:08:17
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CRelatoriosLotacoes
   {
       private string ANO ;
       private string MES ;
       private string IDT_LOTACAO ;
       private string IDT_PROG ;
       private string STATUS ;

	   /// <sumary>
       /// Construtor da classe relatorios_lotacoes
       /// <sumary>
       public CRelatoriosLotacoes()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Relatorios_lotacoes
       /// <sumary>
       public CRelatoriosLotacoes( string ANO, string MES, string IDT_LOTACAO, string IDT_PROG, string STATUS )
       {
          this.ANO = ANO;
          this.MES = MES;
          this.IDT_LOTACAO = IDT_LOTACAO;
          this.IDT_PROG = IDT_PROG;
          this.STATUS = STATUS;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Ano do relatorio mensal de execucao
        /// <sumary>
        public string Ano 
		{
		   get
		   {
		       return this.ANO;
		   }
		   set
		   {
		       this.ANO = value;
		   }
		}
     	/// <sumary>
        /// Mes do relatorio mensal de execucao
        /// <sumary>
        public string Mes 
		{
		   get
		   {
		       return this.MES;
		   }
		   set
		   {
		       this.MES = value;
		   }
		}
     	/// <sumary>
        /// Identificador da lotacao do tecnico
        /// <sumary>
        public string Idt_lotacao 
		{
		   get
		   {
		       return this.IDT_LOTACAO;
		   }
		   set
		   {
		       this.IDT_LOTACAO = value;
		   }
		}
     	/// <sumary>
        /// Identificador do programa
        /// <sumary>
        public string Idt_prog 
		{
		   get
		   {
		       return this.IDT_PROG;
		   }
		   set
		   {
		       this.IDT_PROG = value;
		   }
		}
     	/// <sumary>
        /// Situacao do relatorio mensal: A - Andamento, F - Finalizado
        /// <sumary>
        public string Status 
		{
		   get
		   {
		       return this.STATUS;
		   }
		   set
		   {
		       this.STATUS = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Relatorios_lotacoes.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_RELATORIOS_LOTACOES
			                  ( ANO, MES, IDT_LOTACAO, IDT_PROG, STATUS )
						   values
						      ( :ANO, :MES, :IDT_LOTACAO, :IDT_PROG, :STATUS )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("ANO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("MES",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_LOTACAO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("STATUS",OracleType.Char)); 
				
                command.Parameters["ANO"].Value = this.ANO;
                command.Parameters["MES"].Value = this.MES;
                command.Parameters["IDT_LOTACAO"].Value = this.IDT_LOTACAO;
                command.Parameters["IDT_PROG"].Value = this.IDT_PROG;
                command.Parameters["STATUS"].Value = this.STATUS;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Relatorios_lotacoes.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_RELATORIOS_LOTACOES set 
			                   STATUS = :STATUS
						   where
						       ANO = :ANO and MES = :MES and IDT_LOTACAO = :IDT_LOTACAO and IDT_PROG = :IDT_PROG ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("ANO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("MES",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_LOTACAO",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("IDT_PROG",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("STATUS",OracleType.Char)); 
				
                command.Parameters["ANO"].Value = this.ANO;
                command.Parameters["MES"].Value = this.MES;
                command.Parameters["IDT_LOTACAO"].Value = this.IDT_LOTACAO;
                command.Parameters["IDT_PROG"].Value = this.IDT_PROG;
                command.Parameters["STATUS"].Value = this.STATUS;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Relatorios_lotacoes.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SRX_RELATORIOS_LOTACOES", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("ANO", DbType.Int32, this.ANO));
            tab.adicionarFiltro(new Coluna("MES", DbType.Int32, this.MES));
            tab.adicionarFiltro(new Coluna("IDT_LOTACAO", DbType.Int32, this.IDT_LOTACAO));
            tab.adicionarFiltro(new Coluna("IDT_PROG", DbType.Int32, this.IDT_PROG));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RELATORIOS_LOTACOES order by ANO ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CRelatoriosLotacoes PesquisaporCodigo(string ano, string mes, string idt_lotacao, string idt_prog, string login)
        {
            CRelatoriosLotacoes obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_RELATORIOS_LOTACOES 
                            WHERE ANO = :ANO and MES = :MES and IDT_LOTACAO = :IDT_LOTACAO and IDT_PROG = :IDT_PROG";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":ANO", DbType.String, ano);
				qy.AdicionarParametro(":MES", DbType.String, mes);
				qy.AdicionarParametro(":IDT_LOTACAO", DbType.String, idt_lotacao);
				qy.AdicionarParametro(":IDT_PROG", DbType.String, idt_prog);
               
				qy.Executar();
                obj = new CRelatoriosLotacoes( qy.ObterCampo("ANO"), qy.ObterCampo("MES"), qy.ObterCampo("IDT_LOTACAO"), qy.ObterCampo("IDT_PROG"), qy.ObterCampo("STATUS") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CRelatoriosLotacoes();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login)
        {
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_ANOMESIDT_LOTACAOIDT_PROG() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
		
		static public DataTable ObterPorIDT_LOTACAO(string idt_lotacao)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RELATORIOS_LOTACOES  where IDT_LOTACAO = :IDT_LOTACAO order by ANO ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_LOTACAO", OracleType.Number));
                command.Parameters["IDT_LOTACAO"].Value = idt_lotacao;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		static public DataTable ObterPorIDT_PROG(string idt_prog)
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_RELATORIOS_LOTACOES  where IDT_PROG = :IDT_PROG order by ANO ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
                command.Parameters.Add(new OracleParameter("IDT_PROG", OracleType.Number));
                command.Parameters["IDT_PROG"].Value = idt_prog;
                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection));
                return tbl;
            }
            catch (Exception e)
            {
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
    }
}	

